package cn.nexttec.noteplayer.databases.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import cn.nexttec.noteplayer.models.VideoFileInfo

@Dao
interface VideoFileDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertVideoFile(videoFile: VideoFileInfo)
    @Update
    fun updateVideo(videoInfo: VideoFileInfo)

    @Query("DELETE FROM VideoFile")
    fun clearAll()

    @Query("SELECT * FROM VideoFile")
    fun getVideoFiles(): List<VideoFileInfo>

    @Query("SELECT * FROM VideoFile WHERE Parent = :path AND IsVideoFile = 1")
    fun getVideoFilesByPath(path: String): List<VideoFileInfo>

    @Query("SELECT * FROM VideoFile WHERE Depth = :depth")
    fun getVideoFilesByDepth(depth:Int):List<VideoFileInfo>
    @Query("""
        SELECT * FROM VideoFile 
        WHERE IsDirectory = 1 
           OR (IsVideoFile = 1 AND Depth = 1)
        ORDER BY IsDirectory DESC, Name ASC
    """)
    fun getVideos(): List<VideoFileInfo>?

    @Query("SELECT * FROM VideoFile WHERE VideoMd5 = :md5 LIMIT 1")
    fun getVideoByMd5(md5: String): VideoFileInfo?

    @Query("SELECT * FROM VideoFile WHERE name LIKE '%' || :keyword || '%' COLLATE NOCASE")
    fun search(keyword: String): List<VideoFileInfo>

}